package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("user/order")
@Api(tags = "用户端订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @Autowired
    ShoppingCartMapper shoppingCartMapper;


    @PostMapping("/submit")
    public Result  submit(@RequestBody OrdersSubmitDTO submitDTO){
        log.info("订单参数："+submitDTO);
        OrderSubmitVO submitVO = orderService.submit(submitDTO);


        return Result.success(submitVO);//要返回个OderSubmitVO
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    //day09 项目实战

    /**
     * 根据订单id查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("根据订单id查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result getOrderDetailById(@PathVariable Integer id){
        log.info("订单id："+ id);
        OrderVO OrderVO = orderService.getOrderDetailById(id);
        return Result.success(OrderVO);//OrderVO
    }


    /**
     * 用户端查询历史订单
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @ApiOperation("历史订单")
    @GetMapping("/historyOrders")
    public Result getHistoryOrders(Integer page, Integer pageSize, Integer status){
        log.info("用户端查询历史订单：");
        PageResult historyOrders = orderService.getHistoryOrders(page, pageSize, status);
        return Result.success(historyOrders);
    }


    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancelOrder(@PathVariable Integer id){
        log.info("取消订单的id："+id);
        orderService.CancelOrdersStatus(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result reOrder(@PathVariable Integer id){
        log.info("订单id："+id);
        orderService.reOrder(id);
        return Result.success();
    }


    /**
     * 订单催单
     * @return
     */
    @ApiOperation("催单提醒")
    @GetMapping("/reminder/{id}")
    public Result reMinder(@PathVariable Integer id){
        orderService.reMinderById(id);
        return Result.success();
    }


}
